package com.android.baseconfig.common.views.loading.indicators;

import android.animation.ValueAnimator;
import android.graphics.Canvas;
import android.graphics.Paint;
import com.android.baseconfig.common.views.loading.Indicator;
import java.util.ArrayList;

public class BallBeatIndicator extends Indicator {

    public static final float SCALE = 1.0f;

    public static final int ALPHA = 255;

    private float[] scaleFloats = new float[]{SCALE,
            SCALE,
            SCALE};

    int[] alphas = new int[]{ALPHA,
            ALPHA,
            ALPHA,};

    @Override
    public void draw(Canvas canvas, Paint paint) {
        float circleSpacing = 26;
        float radius = (getWidth() - circleSpacing * 2) / 6;
        float x = getWidth() / 2f - (radius * 2 + circleSpacing);
        float y = getHeight() / 2f;
        for (int i = 0; i < 3; i++) {
            canvas.save();
            float translateX = x + (radius * 2) * i + circleSpacing * i;
            canvas.translate(translateX, y);
            // canvas.scale(scaleFloats[i], scaleFloats[i]);
            paint.setAlpha(alphas[i]);
            canvas.drawCircle(0, 0, radius, paint);
            canvas.restore();
        }
    }

    @Override
    public ArrayList<ValueAnimator> onCreateAnimators() {
        ArrayList<ValueAnimator> animators = new ArrayList<>();
        int[] delays = new int[]{0, 375, 750};
        for (int i = 0; i < 3; i++) {
            final int index = i;
            // ValueAnimator scaleAnim=ValueAnimator.ofFloat(1,0.75f,1);
            // scaleAnim.setDuration(700);
            // scaleAnim.setRepeatCount(-1);
            // scaleAnim.setStartDelay(delays[i]);
            // addUpdateListener(scaleAnim,new ValueAnimator.AnimatorUpdateListener() {
            //     @Override
            //     public void onAnimationUpdate(ValueAnimator animation) {
            //         scaleFloats[index] = (float) animation.getAnimatedValue();
            //         postInvalidate();
            //     }
            // });

            ValueAnimator alphaAnim = ValueAnimator.ofInt(51, 127, 255, 127, 51);
            alphaAnim.setDuration(1500);
            alphaAnim.setRepeatCount(-1);
            alphaAnim.setStartDelay(delays[i]);
            addUpdateListener(alphaAnim, new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator animation) {
                    alphas[index] = (int) animation.getAnimatedValue();
                    postInvalidate();
                }
            });
            // animators.add(scaleAnim);
            animators.add(alphaAnim);
        }
        return animators;
    }


}
